import { ESImageryLayer, ESSceneObject } from "earthsdk3";

export function getCzmCodeFromESImageryLayer(imagery: ESImageryLayer) {
    if (!imagery.url) return undefined;
    const finalUrl = ESSceneObject.context.getStrFromEnv(typeof imagery.url == 'string' ? imagery.url : imagery.url.url);

    const configs: string[] = [];

    configs.push(`url: '${finalUrl}'`);
    const providerName = finalUrl.endsWith('.xml') ? 'TileMapServiceImageryProvider' : 'UrlTemplateImageryProvider';

    const czmCode = /*js*/`
        var imageryProvider = new Cesium.${providerName}({
            ${configs.map(e => `${e}`).join(', \n')}
        });
        viewer.imageryLayers.addImageryProvider(imageryProvider);
        viewer.camera.flyTo({
            destination: Cesium.Rectangle.fromDegrees(west, south, east, north)
        });`;
    return czmCode;
}